#include "include.h"

int sos_brk_move(sos_pcb *pcb, seL4_Word new_brk_vaddr) {
    int err;
    sos_region *region;
    // First we need to find the brk region.
    // This is guaranteed to be the region containing the heap bottom.
    err = sos_resolve_region(&(pcb->regions), SOS_PROCESS_HEAP_START, &region);
    if (err != SOS_REGION_SUCCESS) {
        // This really should not happen - the heap is missing!
        // Not much we can do, but it's possible the process can still do work.
        dprintf(5, "ERROR: PID %d has no heap region.\n", pcb->pid);
        return SOS_PAGE_TABLE_OOM;
    }

    // ... Then we try to resize it.
    seL4_Word start_page = PAGE_SHIFT(region->base);
    seL4_Word end_page = PAGE_SHIFT(new_brk_vaddr);
    // XXX Check math - off by 1?
    err = _resize_region(pcb, region, end_page - start_page);
    return err;
}
